package com.jy.appmarket.newnetwork;

import java.util.Collections;
import java.util.Map;

public interface Cache {
	/**
	 * Retrieves an entry from the cache.
	 * 
	 * @param key
	 *            Cache key
	 * @return An {@link Entry} or null in the event of a cache miss
	 */
    Entry get(String key);

	/**
	 * Adds or replaces an entry to the cache.
	 * 
	 * @param key
	 *            Cache key
	 * @param entry
	 *            Data to store and metadata for cache coherency, TTL, etc.
	 */
    void put(String key, Entry entry);

	/**
	 * Performs any potentially long-running actions needed to initialize the
	 * cache; will be called from a worker thread.
	 */
    void initialize();

	void remove(String key);

	void clear();

	/**
	 * Data and metadata for an entry returned by the cache.
	 */
    class Entry {
		/** The data returned from cache. */
		public byte[] data;

		/** ETag for cache coherency. */
		public String etag;

		/** Date of this response as reported by the server. */
		public long serverDate;

		/** The last modified date for the requested object. */
		public long lastModified;

		/** TTL for this record. */
		public long ttl;

		/** Soft TTL for this record. */
		public long softTtl;

		/** local cache time */
		public long localCacheTime;
		public Map<String, String> responseHeaders = Collections.emptyMap();

		public boolean isExpired() {
			return (Math.abs(System.currentTimeMillis() - this.localCacheTime) > 1000 * 10)
					&& (this.ttl != 0 && this.ttl < System.currentTimeMillis());
		}
	}
}
